//
//  Student.m
//  0921Runtime
//
//  Created by 丁建忠 on 15/12/30.
//  Copyright © 2015年 丁建忠. All rights reserved.
//

#import "Student.h"
#import <objc/runtime.h>

@implementation Teacher

- (void)teach {
    NSLog(@"杨霆锋你好");
}

@end



@implementation Student
//@dynamic name;
//记得给备援接受者初始化,否则无法接受消息
- (instancetype)init
{
    self = [super init];
    if (self) {
        _teacher = [[Teacher alloc] init];
    }
    return self;
}

void newMethod(){
    NSLog(@"修改set方法");
}

+ (BOOL)resolveInstanceMethod:(SEL)sel {
    NSString *selName = NSStringFromSelector(sel);
    if ([selName hasPrefix:@"set"]) {
        class_addMethod(self, sel, (IMP)newMethod, "v@:@");
        return YES;
    }
    return [super resolveInstanceMethod:sel];
}

- (id)forwardingTargetForSelector:(SEL)aSelector {
    NSString *selStr = NSStringFromSelector(aSelector);
    if ([selStr isEqualToString:@"teach"]) {
        return _teacher;
    }
    return [super forwardingTargetForSelector:aSelector];
}
@end
